﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Controls.Menu
{
    public partial class MenuList : UserControl
    {
        private int _listCount;
        private int _currentIndex;
        public MenuList()
        {
            InitializeComponent();
        }

        public List<MenuItem> Menus
        {
            get;set;
        }

        public Bitmap ImageActive { get; set; }
        public Bitmap ImageUnActive { get; set; }
        public int ListCount
        {
            get { return _listCount; }
            set
            {
                if (_listCount == value)
                {
                    return;
                }
                _listCount = value;
                flowLayoutPanel1.Controls.Clear();
                flowLayoutPanel1.Width = 0;
                
                for (int i = 0; i < _listCount; i++)
                {
                    MenuItem tItem = new MenuItem
                    {
                        ImageActive = ImageActive,
                        ImageUnActive = ImageUnActive,
                        IsRadio = true,
                        FlagActive = false,
                        Margin = Padding.Empty,
                        ToolTipText = GetToolTip(i, false),
                    };
                    tItem.OnChecked += TItem_OnChecked;
                    flowLayoutPanel1.Controls.Add(tItem);
                }
                if (CurrentIndex > flowLayoutPanel1.Controls.Count-1)
                {
                    CurrentIndex = 0;
                }
                var item = (flowLayoutPanel1.Controls[CurrentIndex] as MenuItem);
                item.FlagActive = true;
                item.ToolTipText = GetToolTip(CurrentIndex, true);
            }
        }

        string GetToolTip(int index,bool flagSelect)
        {
            
            if (flagSelect)
            {
                return $"第{index}屏幕,该屏幕为当前显示屏幕。";
            }
            else
            {
                return $"第{index}屏幕，点击可切换到当前屏幕进行显示。";
            }
        }

        private void TItem_OnChecked(object sender, EventArgs e)
        {
          for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
            {
                MenuItem item = flowLayoutPanel1.Controls[i] as MenuItem;
                if (flowLayoutPanel1.Controls[i] == sender)
                {
                    CurrentIndex = i;
                    item.ToolTipText = GetToolTip(i, true);
                }
                else
                {
                    item.FlagActive = false;
                    item.ToolTipText = GetToolTip(i, false);
                }
            }
        }

        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {
                if (_currentIndex == value)
                {
                    return;
                }
                _currentIndex = value;
                OnCurrentIndexChanged?.Invoke(this, new IndexChangedEventArgs() {SelIndex = value });
            }
        }

        public event EventHandler<IndexChangedEventArgs> OnCurrentIndexChanged;
    }

    public class IndexChangedEventArgs : EventArgs
    {
        public int SelIndex;
    }
}
